package com.hx.hospital.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hx.common.result.PageResult;
import com.hx.hospital.mapper.DescMapper;
import com.hx.hospital.pojo.Desc;
import com.hx.hospital.service.DescService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class DescServiceImpl implements DescService {

    @Autowired
    private DescMapper descMapper;

    /**
     * 返回全部记录
     * @return
     */
    public List<Desc> findAll() {
        return descMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<Desc> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<Desc> descs = (Page<Desc>) descMapper.selectAll();
        return new PageResult<Desc>(descs.getTotal(),descs.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<Desc> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return descMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<Desc> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<Desc> descs = (Page<Desc>) descMapper.selectByExample(example);
        return new PageResult<Desc>(descs.getTotal(),descs.getResult());
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public Desc findById(Long id) {
        return descMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param desc
     */
    public void add(Desc desc) {
        descMapper.insert(desc);
    }

    /**
     * 修改
     * @param desc
     */
    public void update(Desc desc) {
        descMapper.updateByPrimaryKeySelective(desc);
    }

    /**
     *  删除
     * @param id
     */
    public void delete(Long id) {
        descMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Desc.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 医院名字
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }
            // 地址
            if(searchMap.get("adress")!=null && !"".equals(searchMap.get("adress"))){
                criteria.andLike("adress","%"+searchMap.get("adress")+"%");
            }
            // 电话
            if(searchMap.get("phone")!=null && !"".equals(searchMap.get("phone"))){
                criteria.andLike("phone","%"+searchMap.get("phone")+"%");
            }
            // 公交信息
            if(searchMap.get("busMessage")!=null && !"".equals(searchMap.get("busMessage"))){
                criteria.andLike("busMessage","%"+searchMap.get("busMessage")+"%");
            }


        }
        return example;
    }

}
